package io.github.openfeign.soap;

import feign.Feign;
import feign.Headers;
import feign.RequestLine;
import feign.jaxb.JAXBContextFactory;
import feign.soap.SOAPDecoder;
import feign.soap.SOAPEncoder;
import okhttp3.OkHttpClient;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.concurrent.TimeUnit;

public class SoapTest1 {
    public interface WeacherSoap {

        @RequestLine("POST /WebServices/WeatherWebService.asmx")
        @Headers({"Content-Type:text/xml; charset=utf-8","SOAPAction:http://WebXml.com.cn/getSupportCity"})
        public GetSupportCityResponse getSupportCities(GetSupportCity byProvinceName);

    }

    @XmlRootElement(namespace = "http://WebXml.com.cn/")
    static class GetSupportCity {

        String byProvinceName;

        public String getByProvinceName() {
            return byProvinceName;
        }

        @XmlElement(namespace = "http://WebXml.com.cn/")
        public void setByProvinceName(String byProvinceName) {
            this.byProvinceName = byProvinceName;
        }
    }

    public static void main(String[] args) throws Exception {
        System.setProperty("http.proxyHost","127.0.0.1");
        System.setProperty("http.proxyPort","8089");
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(1, TimeUnit.SECONDS)
                .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8089)))
                .build();
        JAXBContextFactory jaxbFactory = new JAXBContextFactory.Builder()
                .withMarshallerJAXBEncoding("UTF-8")
//                .withMarshallerSchemaLocation("http://www.webxml.com.cn http://apihost/schema.xsd")
                .build();
//        JAXBContextFactory jAXBContextFactory = new JAXBContextFactory.Builder().build();
        SOAPEncoder encoder = new SOAPEncoder.Builder().withJAXBContextFactory(jaxbFactory).build();
        SOAPDecoder decoder = new SOAPDecoder.Builder().withJAXBContextFactory(jaxbFactory).build();
        WeacherSoap client = Feign.builder()
//                .client(new feign.okhttp.OkHttpClient(okHttpClient))
                .encoder(encoder)
                .decoder(decoder)
//                .errorDecoder((methodKey,resp) -> {
//                    System.out.println(methodKey);
//                    return new RuntimeException("xxxx");
//                })
                .target(WeacherSoap.class, "http://www.webxml.com.cn/");
        GetSupportCity getSupportCity = new GetSupportCity();
        getSupportCity.setByProvinceName("ALL");
        GetSupportCityResponse supportCities = client.getSupportCities(getSupportCity);
//        System.out.println(supportCities);
        supportCities.getGetSupportCityResult().forEach(var -> {
            System.out.println(var);
        });
//        Marshaller marshaller = jaxbFactory.createMarshaller(String.class);

//        Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
//        Marshaller marshaller = jaxbFactory.createMarshaller(GetSupportCity.class);
//        GetSupportCity getSupportCity = new GetSupportCity();
//        getSupportCity.setByProvinceName("ALL");
//        marshaller.marshal(getSupportCity, document);
//        System.out.println(document);
    }
}
